/**
 * 
 */
package com.roc.web.action.terminal;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.expression.spel.ast.Operator;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.Estidinfo;
import com.roc.pojo.Operators;
import com.roc.pojo.OrgBean;
import com.roc.pojo.YjPosInfo;
import com.roc.service.IMerchantServices;
import com.roc.service.IPagenitionService;
import com.roc.service.IorgService;
import com.roc.service.ServiceException;
import com.roc.util.Constance;
import com.roc.util.constant.ConfigProperties;
import com.roc.util.constant.Constant;
import com.roc.util.constant.Terminal;
import com.roc.util.constant.TerminalBrand;
import com.roc.util.constant.TerminalFlag;
import com.roc.util.constant.TerminalName;
import com.roc.util.constant.TerminalType;

/**
 * @author dxpeng
 * 
 */
@Controller
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Namespace("/termInfo")
public class TermSnInfoAction  extends PaginationBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	static Logger logger = LogManager.getLogger(TermSnInfoAction.class);

	private Long psamCardStartNo;
	private Long psamCardEndNo;
	private Long totalNum;
	private TermSnInfoSearchCondition searchCondition;
	
	private List<OrgBean> orgList=new ArrayList<OrgBean>();
	@Autowired
	IPagenitionService pagenitionService;
	
	@Autowired
	private IorgService orgService;
	
	@Action(value = "termSnInfo",
			results = { @Result(location = "/pages/terminal/termSnInfo.jsp") })
	public String termSnInfoAction() throws Exception {
		org();
		return SUCCESS;
	}
	
	@Action(value = "queryTermPosInfo", results = { @Result(name = "success", location = "/pages/terminal/termSnInfo.jsp")
    })
	//查询终端序列号
	public String queryTermPosInfoAction() throws Exception{
		Operators loginOperator = (Operators) ActionContext.getContext().getSession().get(Constant.SESSION_LOGIN_MERCHANT);
		if(searchCondition==null){
			searchCondition=(TermSnInfoSearchCondition)getSearchConditionFromSession();
			if(searchCondition==null){
				searchCondition=new TermSnInfoSearchCondition();
			}
		}
		try {
			org();
			searchCondition.setAgentId(Constance.Null2EmptyTrim(loginOperator.getId()));
			searchCondition.setIsUsed(Terminal.BIND_TWO.getCode());
			int rows=pagenitionService.countSQL(searchCondition);
			searchRsList=pagenitionService.findAllSQL(searchCondition, calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
	
			List<YjPosInfo> posList= new ArrayList<YjPosInfo>();
			if(searchRsList!=null&&searchRsList.size()>0){
				Iterator iterator=searchRsList.iterator();
				while (iterator.hasNext()) {
					Object[] obj= (Object[])iterator.next();
					YjPosInfo pos=new YjPosInfo(); 
					pos.setSerialNum(Constance.Null2EmptyTrim(obj[0]));
					pos.setPosBrand(TerminalBrand.valueOfCode(Constance.Null2EmptyTrim(obj[1])).getName());
					pos.setPosType(TerminalType.valueOfCode(Constance.Null2EmptyTrim(obj[2])).getName());
					pos.setPosName(Constance.Null2EmptyTrim(obj[3]));
					pos.setOrgCode(Constance.Null2EmptyTrim(obj[4]));
					pos.setIsBind(Terminal.valueOfCode(Constance.Null2EmptyTrim(obj[5])).getName());
					pos.setIsUsed(Terminal.valueOfCode(Constance.Null2EmptyTrim(obj[6])).getName());
					posList.add(pos);
				}
			}
			searchRsList.clear();
			searchRsList.addAll(posList);
			dealWithTheListSpecially(searchRsList);
			getRequest().setAttribute("posList", posList);
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute(SEARCH_RS_LIST, searchRsList);
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			setSearchToConditionSession(searchCondition);
			
		} catch (ServiceException e) {
			
			logger.error(e.getMessage(),e);
			addActionError(e.getMessage());
			
		}
		
		return SUCCESS;
	}
	
	private void org(){
		try{
			Map<String,String> brandMap=ConfigProperties.getPOSBrand();
			Map<String,String> nameMap=ConfigProperties.getPOSName();
			Map<String,String> typeMap=ConfigProperties.getPOSType();
			getRequest().setAttribute("brandMap", brandMap);
			getRequest().setAttribute("nameMap", nameMap);
			getRequest().setAttribute("typeMap", typeMap);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	protected void dealWithTheListSpecially(List<Object> list) {
		// TODO Auto-generated method stub
		super.dealWithTheListSpecially(list);
		Map<String, String> roleTypes=ConfigProperties.getRoleTypeProperties();
		List<Object> tempList=new ArrayList<Object>();
		for(Object obj:list){
			YjPosInfo pos=(YjPosInfo)obj;
			for(OrgBean bean:orgList){
				if(pos.getOrgCode().equals(bean.getOrg_code())){
					pos.setOrgName(bean.getOrg_name());
				}
			}
			
			tempList.add(pos);
		}
		searchRsList=tempList;
	}
	
	@Override
	protected String getInitPageSize() {
		// TODO Auto-generated method stub
		return Constance.PAGE_MAX_SIZE+"";
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		// TODO Auto-generated method stub
		return (TermSnInfoSearchCondition)getSession().getAttribute(SEARCH_SESSION_BEAN_TERM_SN_INFO);
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		// TODO Auto-generated method stub
		return searchCondition;
	}

	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		this.setSearchConditionToSubClass(searchCondition);
		
	}

	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		getSession().setAttribute(SEARCH_SESSION_BEAN_TERM_SN_INFO, searchCondition);
	}

	
	public Long getPsamCardStartNo() {
		return psamCardStartNo;
	}

	public void setPsamCardStartNo(Long psamCardStartNo) {
		this.psamCardStartNo = psamCardStartNo;
	}

	public Long getPsamCardEndNo() {
		return psamCardEndNo;
	}

	public void setPsamCardEndNo(Long psamCardEndNo) {
		this.psamCardEndNo = psamCardEndNo;
	}

	public Long getTotalNum() {
		return totalNum;
	}

	public void setTotalNum(Long totalNum) {
		this.totalNum = totalNum;
	}

	public List<OrgBean> getOrgList() {
		return orgList;
	}

	public void setOrgList(List<OrgBean> orgList) {
		this.orgList = orgList;
	}

	public TermSnInfoSearchCondition getSearchCondition() {
		return searchCondition;
	}

	public void setSearchCondition(TermSnInfoSearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}



	
	
	

}
